package com.qianyucc.blog.controller.comm;

import cn.hutool.core.util.*;
import com.qianyucc.blog.exception.*;
import com.qianyucc.blog.global.*;
import com.qianyucc.blog.mapper.*;
import com.qianyucc.blog.model.dto.*;
import com.qianyucc.blog.model.entity.*;
import com.qianyucc.blog.model.vo.*;
import com.qianyucc.blog.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.validation.annotation.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author lijing
 * @e-mail 1413979079@qq.com
 * @date 2020-04-25 13:17
 * @description 博客初始化
 */
@RestController
@RequestMapping("/init")
public class InitController {
    @Autowired
    private ConfigMapper configMapper;
    @Autowired
    private ConfigService configService;

    @GetMapping("/check")
    public ResultVO check() {
        List<ConfigDO> all = configMapper.findAll();
        if (all.isEmpty()) {
            return ResultVO.error("博尚未初始化");
        }
        return ResultVO.ok("博客已经初始化");
    }

    @PostMapping("/user")
    public ResultVO user(@RequestBody @Validated AdminUserDTO user) {
        configMapper.insert(new ConfigDO(Constants.ConfigKey.USERNAME, user.getUsername()));
        configMapper.insert(new ConfigDO(Constants.ConfigKey.PASSWORD, user.getPassword()));
        configMapper.insert(new ConfigDO(Constants.ConfigKey.ADMIN_TOKEN, StrUtil.EMPTY));
        return ResultVO.ok("注册成功");
    }

    @PostMapping("/blog")
    public ResultVO blog(@RequestBody @Validated BlogInfoDTO info) {
        configMapper.insert(new ConfigDO(Constants.ConfigKey.BLOG_NAME, info.getBlogName()));
        configMapper.insert(new ConfigDO(Constants.ConfigKey.BLOG_DESC, info.getBlogDesc()));
        configMapper.insert(new ConfigDO(Constants.ConfigKey.BLOG_ICON, info.getBlogIcon()));
        return ResultVO.ok("博客信息初始化完成");
    }

    @PostMapping("/profile")
    public ResultVO profile(@RequestBody @Validated ProfileDTO profile) {
        configMapper.insert(new ConfigDO(Constants.ConfigKey.NAME, profile.getName()));
        configMapper.insert(new ConfigDO(Constants.ConfigKey.PEN_NAME, profile.getPenName()));
        configMapper.insert(new ConfigDO(Constants.ConfigKey.MOTTO, profile.getMotto()));
        configMapper.insert(new ConfigDO(Constants.ConfigKey.INTRODUCTION, profile.getIntroduction()));
        configMapper.insert(new ConfigDO(Constants.ConfigKey.AVATAR, profile.getAvatar()));
        configMapper.insert(new ConfigDO(Constants.ConfigKey.WEBSITES, StrUtil.EMPTY));
        configService.initConfig();
        return ResultVO.ok("个人信息初始化完成");
    }
}
